package element.synt;

import element.base.Synt;
import element.base.TreeNode;
import element.base.Wordtype;

import java.util.ArrayList;

//ConstInitVal → ConstExp | '{' [ ConstInitVal { ',' ConstInitVal } ] '}'
public class ConstInitVal extends Synt {
    public String getvalue0() {
        if (this.getNodes().get(0) instanceof ConstExp) {
            int num = ((ConstExp) this.getNodes().get(0)).calcu();
            return String.valueOf(num);
        } else {
            return null;
        }
    }

    public ArrayList<String> getvalue1(int len1) {
        ArrayList<TreeNode> nodes = this.getNodes();
        if (nodes.get(0).isWord(Wordtype.LBRACE)) {
            ArrayList<String> returns = new ArrayList<>();
            int i = 0;
            for (TreeNode node: nodes) {
                if (node instanceof ConstInitVal) {
                    returns.add(((ConstInitVal) node).getvalue0());
                    i++;
                }
            }
            while (i < len1) {
                returns.add("0");
                i++;
            }
            return returns;
        } else {
            return null;
        }
    }

    public ArrayList<ArrayList<String>> getValue2(int len1, int len2) {
        ArrayList<TreeNode> nodes = this.getNodes();
        if (nodes.get(0).isWord(Wordtype.LBRACE)) {
            ArrayList<ArrayList<String>> returns = new ArrayList<>();
            int i = 0;
            for (TreeNode node: nodes) {
                if (node instanceof ConstInitVal) {
                    returns.add(((ConstInitVal) node).getvalue1(len2));
                    i++;
                }
            }
            while (i < len1) {
                ArrayList<String> inside = new ArrayList<>();
                for (int j = 0; j < len2; j++) {
                    inside.add("0");
                }
                returns.add(inside);
            }
            return returns;
        } else {
            return null;
        }
    }
}
